﻿using System;

namespace WGT
{
    public partial class Dialog
    {
        private int parentid;

        public void Panel(pos p, Action content, params string[] attrs)
        {
            //vars
            int id = 0;
            string[] oattrs = null;
            pos op = null;

            string atf = AttrUtil.First(attrs);
            string ats = AttrUtil.Second(attrs);

            //remember
            int _gx = gx;
            int _gy = gy;
            int _gxSave = gxSave;
            int _gySave = gySave;
            bool _bH = bHorizontal;

            var tp = parentid;
            parentid = id;

            //initialize
            gx = gy = 0;
            gxSave = gySave = 0;
            bHorizontal = false;

            GetPut(ref op, ref p);
            GetPut(ref oattrs, ref attrs);

            if (mode == MODE.SHOW)
            {
                id = iNextId++;
                L(id);
                U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                UP(id.ToString(), atf);
                U(@"sc('{0}','{1}');", id, ats);
            }

            GetPut(ref id, ref id);
            string sid = id.ToString();

            if (mode == MODE.UPDATE)
            {
                L(id);
                if (oattrs != attrs)
                {
                    UP(sid.ToString(), atf);
                    U(@"sc('{0}','{1}');", sid, ats);
                }
                if (!p.Equals(op))
                {
                    U(@"p({0},{1},{2},{3},{4});", id, p.y, p.x, p.w, p.h);
                }
            }
            else if (mode == MODE.ERASE)
            {
                UP(sid, false.ToVisible());
            }
           
            //show inner content
            if (content != null) content();

            //restore old
            gx = _gx;
            gy = _gy;
            gxSave = _gxSave;
            gySave = _gySave;
            bHorizontal = _bH;
            parentid = tp;

            // return to parent
            if (mode == MODE.SHOW) U(@"l('{0}');", parentid);

            if (mode != MODE.ERASE)
            {
                if (bHorizontal) gx += p.w;
                else gy += p.h;
            }
        }
    }
}